#!/usr/bin/python
"""
This is a simple example to demostrate rucio with containernet.
"""
from mininet.net import Containernet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import info, setLogLevel
from container import Container
import os

# The absolute path of the rucio repo, note this is the abs path in host
RUCIO_DIR = os.path.abspath(os.path.dirname(__file__) + '../../../docker/rucio')

setLogLevel('info')

net = Containernet(controller=Controller)

info('*** Adding controller\n')
net.addController('c0')

info('*** Adding rucio docker containers\n')
# Note: containernet ignores CMD of Dockerfile, the cmd should be explictly specified in dcmd, else /bin/bash will be the default
rucio = net.addHost('rucio', cls=Container, dns=['10.0.0.11'], dimage="openalto/rucio-dev", volumes=[RUCIO_DIR+'/tools:/opt/rucio/tools:rw', RUCIO_DIR+'/bin:/opt/rucio/bin:rw', RUCIO_DIR+'/lib:/opt/rucio/lib:rw'], environment={'RDBMS': 'postgres14'})
ruciodb = net.addHost(name='ruciodb', cls=Container, dimage='postgres:14', dns=['10.0.0.11'], dcmd='docker-entrypoint.sh postgres', environment={'POSTGRES_USER': 'rucio', 'POSTGRES_DB': 'rucio', 'POSTGRES_PASSWORD': 'secret'})
minio = net.addHost(name='minio', cls=Container, dimage='minio/minio', dns=['10.0.0.11'], dcmd='/usr/bin/docker-entrypoint.sh minio server /data', volumes=[RUCIO_DIR+'/etc/certs/hostcert_xrd4.pem:/tmp/xrdcert.pem:Z', RUCIO_DIR+'/etc/certs/hostcert_xrd4.key.pem:/tmp/xrdkey.pem:Z'], environment={'MINIO_ACCESS_KEY': 'admin', 'MINIO_SECRET_KEY': 'password'})
fts = net.addHost(name='fts', cls=Container, dimage='rucio/fts', dcmd='/docker-entrypoint.sh', dns=['10.0.0.11'],)
ftsdb = net.addHost(name='ftsdb', cls=Container, dimage='mysql:8', dns=['10.0.0.11'], dcmd='/entrypoint.sh mysqld --user=root --default-authentication-plugin=mysql_native_password', environment={'MYSQL_USER': 'fts', 'MYSQL_PASSWORD': 'fts', 'MYSQL_ROOT_PASSWORD': 'fts', 'MYSQL_DATABASE': 'fts'})
activemq = net.addHost(name='activemq', cls=Container, dimage='webcenter/activemq:latest', dcmd='/app/run.sh', dns=['10.0.0.11'], environment={'ACTIVEMQ_CONFIG_NAME': 'activemq', 'ACTIVEMQ_CONFIG_DEFAULTACCOUNT': False, 'ACTIVEMQ_USERS_fts': 'supersecret', 'ACTIVEMQ_GROUPS_writes': 'fts', 'ACTIVEMQ_USERS_receiver': 'supersecret', 'ACTIVEMQ_GROUPS_reads': 'receiver', 'ACTIVEMQ_CONFIG_SCHEDULERENABLED': True})
ssh1 = net.addHost(name='ssh1', cls=Container, dimage='rucio/ssh', dcmd='/docker-entrypoint.sh', dns=['10.0.0.11'], volumes=[RUCIO_DIR+'/etc/certs/ssh/ruciouser_sshkey.pub:/tmp/sshkey.pub:Z'])
grafana = net.addHost(name='grafana', cls=Container, dimage='grafana/grafana:latest', dcmd='/run.sh', dns=['10.0.0.11'],)
odl = net.addHost(name='odl', cls=Container, dimage='openalto/odl:0.8.4', dcmd="/bin/bash -c '/opt/opendaylight/bin/start && tail -f /dev/null'", dns=['10.0.0.11'],)
graphite = net.addHost(name='graphite', cls=Container, dimage='graphiteapp/graphite-statsd', dns=['10.0.0.11'])

# DNS is necessary for rucio
dns = net.addHost('dns', cls=Container, ip='10.0.0.11', dimage="defreitas/dns-proxy-server", dcmd='/app/dns-proxy-server', publish_all_ports=True, volumes=['/var/run/docker.sock:/var/run/docker.sock'])
    
info('*** Adding xrd docker containers\n')
xrd1 = net.addHost('xrd1', cls=Container, dns=['10.0.0.11'], dimage="rucio/xrootd", dcmd='/docker-entrypoint.sh', environment={'XRDPORT': 1094}, volumes=[RUCIO_DIR+'/etc/certs/hostcert_xrd1.pem:/tmp/xrdcert.pem:Z', RUCIO_DIR+'/etc/certs/hostcert_xrd1.key.pem:/tmp/xrdkey.pem:Z'])
xrd2 = net.addHost('xrd2', cls=Container, dns=['10.0.0.11'], dimage="rucio/xrootd", dcmd='/docker-entrypoint.sh', environment={'XRDPORT': 1095}, volumes=[RUCIO_DIR+'/etc/certs/hostcert_xrd2.pem:/tmp/xrdcert.pem:Z', RUCIO_DIR+'/etc/certs/hostcert_xrd2.key.pem:/tmp/xrdkey.pem:Z'])
xrd3 = net.addHost('xrd3', cls=Container, dns=['10.0.0.11'], dimage="rucio/xrootd", dcmd='/docker-entrypoint.sh', environment={'XRDPORT': 1096}, volumes=[RUCIO_DIR+'/etc/certs/hostcert_xrd3.pem:/tmp/xrdcert.pem:Z', RUCIO_DIR+'/etc/certs/hostcert_xrd3.key.pem:/tmp/xrdkey.pem:Z'])
xrd4 = net.addHost('xrd4', cls=Container, dns=['10.0.0.11'], dimage="rucio/xrootd", dcmd='/docker-entrypoint.sh', environment={'XRDPORT': 1097}, volumes=[RUCIO_DIR+'/etc/certs/hostcert_xrd4.pem:/tmp/xrdcert.pem:Z', RUCIO_DIR+'/etc/certs/hostcert_xrd4.key.pem:/tmp/xrdkey.pem:Z'])

info('*** Adding switches\n')
s1 = net.addSwitch('s1')
s2 = net.addSwitch('s2')
s3 = net.addSwitch('s3')
s4 = net.addSwitch('s4')
s5 = net.addSwitch('s5')

info('*** Creating links\n')
# rucio services
net.addLink(rucio, s3)
net.addLink(ruciodb, s3)
net.addLink(dns, s3)
net.addLink(minio, s3)
net.addLink(fts, s3)
net.addLink(ftsdb, s3)
net.addLink(activemq, s3)
net.addLink(ssh1, s3)
net.addLink(grafana, s3)
net.addLink(odl, s3)
net.addLink(graphite, s3)
net.addLink(xrd1, s1)
net.addLink(xrd2, s2)
net.addLink(xrd3, s4)
net.addLink(xrd4, s5)

# switch links
net.addLink(s1, s3, cls=TCLink, delay='25ms', bw=1)
net.addLink(s2, s3, cls=TCLink, delay='25ms', bw=1)
net.addLink(s3, s4, cls=TCLink, delay='25ms', bw=5)
net.addLink(s4, s5, cls=TCLink, delay='50ms', bw=2)

info('*** Starting network\n')
net.start()

for host in net.hosts:
    host.ready = True

info('*** Running CLI\n')
CLI(net)

info('*** Stopping network')
net.stop()